﻿using PlayListSet.Comm;
using PlayListSet.Entity;
using PlayListSet.UI.ValueConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PlayListSet.UI
{
    public delegate void DeleteItem(object sender,PlayInfoBase item);
 
    /// <summary>
    /// PlayInfoItem.xaml 的交互逻辑
    /// </summary>
    public partial class PlayInfoItem : UserControl,IPlayInfo
    {
        public PlayInfo PlayInfo { set; get; }
        public event DeleteItem OnDelete;
        public PlayInfoItem()
        {
            InitializeComponent();
            //PlayInfo.PropertyChanged += PlayInfo_PropertyChanged;
            this.Loaded += PlayInfoItem_Loaded;
        }
        IntToStr intToStr;
        private void PlayInfoItem_Loaded(object sender, RoutedEventArgs e)
        {
          
            this.SetBinding(OpacityProperty, new Binding("Opacity") { Source = PlayInfo });
            itemName.SetBinding(TextBlock.TextProperty, new Binding("Name") { Source = PlayInfo });
            tb_fileName.SetBinding(TextBlock.TextProperty, new Binding("SourceFileName") { Source = PlayInfo });
            intToStr = new IntToStr();
            tbType.SetBinding(TextBlock.TextProperty, new Binding("SourceType") { Source = PlayInfo  ,
                Converter= intToStr,ConverterParameter="视频：;图片：;音频：;NDI：;Spout：" });

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            OnDelete?.Invoke(this,PlayInfo);
        }
        public long GetPlayInfoId()
        {
            return PlayInfo.ID;
        }
 

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //this.Focus();
            AppControl.Instance.CopySelect = PlayInfo;
        }

        private void copy_click(object sender, RoutedEventArgs e)
        {
            try
            {
                 AppHelp.CopyToClipboard(new AppHelp.ATSCopyClipboardData() { CopyType = AppHelp.CopyType.PlayInfoItem, Data = PlayInfo });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            e.Handled = true;
        }

        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {
            AppControl.Instance.CopySelect = null;
        }

        

        private void we_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            we.ReleaseMouseCapture();
            e.Handled = true;
        }

        private void we_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            we.CaptureMouse();
            e.Handled = true;
        }
        public event MouseEventHandler RightMouseEventHandler;
        private void we_MouseMove(object sender, MouseEventArgs e)
        {
            RightMouseEventHandler?.Invoke(this, e);
        }

        private void UserControl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //       < Grid.ContextMenu >
            //    < ContextMenu >
            //        < MenuItem Header = "复制" Click = "copy_click" ></ MenuItem >

            //        < MenuItem Header = "删除" Click = "MenuItem_Click" ></ MenuItem >
            //    </ ContextMenu >
            //</ Grid.ContextMenu >
            ContextMenu contextMenu = new ContextMenu();
            var c = new MenuItem { Header = "复制" };
           c.Click += copy_click;

            contextMenu.Items.Add(c);
            var d = new MenuItem { Header = "删除" };
            d.Click += MenuItem_Click;

            contextMenu.Items.Add(d);
            this.ContextMenu = contextMenu;
            this.ContextMenu.IsOpen = true;
        }
        //private void PlayInfo_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{

        //}
    }
}
